<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="版本号"
        >
          <a-input placeholder="版本号"
                   v-decorator="[ 'versionNo', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="更新内容"
        >
          <a-input type="textarea" placeholder="更新内容"
                   v-decorator="[ 'description', {rules: [{ required: false, message: '请输入。。。' }] }]"/>
        </a-form-item>


        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="更新模式">
          <a-select placeholder="更新模式"
                    v-decorator="[ 'isMust', {rules: [{ required: true, message: '请选择适用商户' }]}]">
            <a-select-option key="0">选择更新</a-select-option>
            <a-select-option key="1">强制更新</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="下载地址"
        >
          <a-input placeholder="下载地址" :disabled="true"
                   v-decorator="[ 'downUrl', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="安卓应用程序"
        >
          <a-upload
            :action="this.$baseURL+'/common/upload/uploadFile'"
            listType="picture-card"
            :fileList="fileList"
            @change="uploadChange"
          >
            <div v-if="fileList.length < 1">
              <a-icon type="plus"/>
              <div class="ant-upload-text">上传安卓应用程序</div>
            </div>
          </a-upload>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { save, deleteFile } from '@/api/system/version'
  import pick from 'lodash.pick'
  export default {
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),

        isDisabled: false,
        fileList: []
      }
    },
    //--############  data() ###############-->

    created() {

    },

    //--############  methods() ###############-->
    methods: {
      add() {
        this.isDisabled = false
        this.visible = true
      },
      edit(record) {
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true
        this.mdl.isMust = this.mdl.isMust + ''
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'versionNo', 'description', 'isMust', 'downUrl'))
        })

      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl = {}
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id
            values.fileName = this.mdl.fileName

            console.log('form values', values)
            _this.confirmLoading = true
            // 模拟后端请求 2000 毫秒延迟
            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              } else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      uploadChange(info) {
        const _this = this;
        this.fileList = info.fileList
        if (info.file.status === 'done') {
          this.fileList.pop()
          this.fileList.push(info.file)
          this.$message.success(`${info.file.name} 上传成功`)
          this.mdl.downUrl = info.file.response.url
          this.mdl.fileName = info.file.response.name
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.mdl, 'versionNo', 'description', 'isMust', 'downUrl'))
          })
        } else if (info.file.status === 'error') {
          this.$message.error(`${info.file.name} 上传失败`)
        } else if (info.file.status === 'removed') {
          let name
          if (this.mdl.fileName != undefined) {
            name = this.mdl.fileName
          } else {
            name = info.file.response.name
          }
          if (name != undefined || name != null) {
            deleteFile(name).then(res => {
              _this.form.setFieldsValue({"downUrl":''})
            })
          }
        }
      }
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
